home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / UViewServer.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  11.8 KB  |  462 lines  |  [TEXT/MPS ]

  1. // UViewServer.cp
  2. // Copyright © 1987-1991 by Apple Computer Inc.    All rights reserved.
  3.  
  4. #ifndef __STDIO__
  5. #include <StdIo.h>
  6. #endif
  7.  
  8. #ifndef __UGEOMETRY__
  9. #include <UGeometry.h>
  10. #endif
  11.  
  12. #ifndef __UWINDOW__
  13. #include <UWindow.h>
  14. #endif
  15.  
  16. #ifndef __UMACAPPUTILITIES__
  17. #include <UMacAppUtilities.h>
  18. #endif
  19.  
  20. #ifndef __UMACAPPGLOBALS__
  21. #include <UMacAppGlobals.h>
  22. #endif
  23.  
  24. #ifndef __RESOURCES__
  25. #include <Resources.h>
  26. #endif
  27.  
  28. #ifndef __USCROLLER__
  29. #include <UScroller.h>
  30. #endif
  31.  
  32. #ifndef __UAPPLICATION__
  33. #include <UApplication.h>
  34. #endif
  35.  
  36. #ifndef __UERRORMGR__
  37. #include <UErrorMgr.h>
  38. #endif
  39.  
  40. #ifndef __UVIEWSERVER__
  41. #include <UViewServer.h>
  42. #endif
  43.  
  44. //--------------------------------------------------------------------------------------------------
  45. // global declarations for globals defined in interface
  46. TViewServer* gViewServer;
  47.  
  48. //--------------------------------------------------------------------------------------------------
  49. #pragma segment MAOpen
  50.  
  51. pascal void TViewServer::Initialize(void)        // override 
  52. {
  53.     inherited::Initialize();
  54.  
  55. }
  56.  
  57. //--------------------------------------------------------------------------------------------------
  58. #pragma segment MAOpen
  59.  
  60. pascal void TViewServer::IViewServer()
  61. {
  62.     this->IObject();
  63.  
  64.     gViewServer = this;                            // set the global reference.
  65. }
  66.  
  67. //--------------------------------------------------------------------------------------------------
  68. #pragma segment MAClose
  69.  
  70. pascal void TViewServer::Free(void)                // override 
  71. {
  72.     gViewServer = NULL;                            // NIL out the global reference.
  73.  
  74.     inherited::Free();
  75. }
  76.  
  77. //--------------------------------------------------------------------------------------------------
  78. #pragma segment MAOpen
  79.  
  80. pascal TWindow* TViewServer::NewPaletteWindow(short itsRsrcID,
  81.                                         Boolean wantHScrollBar,
  82.                                         Boolean wantVScrollBar,
  83.                                         TDocument* itsDocument,
  84.                                         TView* itsMainView,
  85.                                         TView* itsPaletteView,
  86.                                         short sizePalette,
  87.                                         VHSelect whichWay)
  88.  
  89. {
  90.     TWindow * aWindow;
  91.     TScroller * aScroller = NULL;
  92.     FailInfo fi;
  93.     VPoint itsSize;
  94.     VPoint itsLocation;
  95.     VRect sBarOffsets;
  96.  
  97.     VOLATILE(aWindow);
  98.     
  99.     aWindow = NewTWindow(itsRsrcID, itsDocument);
  100.  
  101.     (aWindow->fResizeLimits[topLeft])[whichWay] += sizePalette;
  102.  
  103.     if (fi.Try())
  104.     {
  105.  
  106.         aWindow->AddSubView(itsPaletteView);
  107.  
  108.         itsLocation = gZeroVPt;
  109.         itsLocation[whichWay] = sizePalette;
  110.         if (wantHScrollBar || wantVScrollBar)
  111.         {
  112.             sBarOffsets = gZeroVRect;
  113.             itsSize = aWindow->fSize;
  114.             if (wantHScrollBar)
  115.             {
  116.                 itsSize.v -= kSBarSizeMinus1;
  117.                 if (!wantVScrollBar)
  118.                     sBarOffsets.right = -kSBarSizeMinus1;
  119.             }
  120.             if (wantVScrollBar)
  121.             {
  122.                 itsSize.h -= kSBarSizeMinus1;
  123.                 if (!wantHScrollBar)
  124.                     sBarOffsets.bottom = -kSBarSizeMinus1;
  125.             }
  126.             itsSize[whichWay] -= sizePalette;
  127.  
  128.             FailInfo fi2;
  129.             aScroller = new TScroller;
  130.             aScroller->IScroller(aWindow, itsLocation, itsSize, sizeRelSuperView, sizeRelSuperView, gZeroVPt, wantHScrollBar, wantVScrollBar);
  131.             aScroller->fSBarOffsets = sBarOffsets;
  132.             aScroller->AddSubView(itsMainView);
  133.         }
  134.         else
  135.             aWindow->AddSubView(itsMainView);
  136.  
  137.         aWindow->SetTarget(itsMainView);
  138.  
  139.         // make frames be the right size 
  140.         aWindow->Resize(VPoint(aWindow->fWMgrWindow->portRect[botRight] - aWindow->fWMgrWindow->portRect[topLeft]), kDontInvalidate);
  141.  
  142.         fi.Success();
  143.     }
  144.     else    // Recover
  145.     {
  146.         aWindow = (TWindow *)FreeIfObject(aWindow);
  147.         fi.ReSignal();                            // Optional. Omit to continue.
  148.     }
  149.  
  150.     return aWindow;
  151. }
  152.  
  153. //--------------------------------------------------------------------------------------------------
  154. #pragma segment MAOpen
  155.  
  156. pascal TWindow* TViewServer::NewSimpleWindow(short itsRsrcID,
  157.                                        Boolean wantHScrollBar,
  158.                                        Boolean wantVScrollBar,
  159.                                        TDocument* itsDocument,
  160.                                        TView* itsView)
  161.  
  162. {
  163.     TWindow * aWindow;
  164.     TScroller * aScroller = NULL;
  165.     FailInfo fi;
  166.     VPoint itsSize;
  167.     VRect sBarOffsets;
  168.  
  169.     VOLATILE(aWindow);
  170.  
  171.     aWindow = NewTWindow(itsRsrcID, itsDocument);
  172.  
  173.     if (fi.Try())
  174.     {
  175.  
  176.         if (wantHScrollBar || wantVScrollBar)
  177.         {
  178.             sBarOffsets = gZeroVRect;
  179.             itsSize = aWindow->fSize;
  180.             if (wantHScrollBar)
  181.             {
  182.                 itsSize.v -= kSBarSizeMinus1;
  183.                 if (!wantVScrollBar)
  184.                     sBarOffsets.right = -kSBarSizeMinus1;
  185.             }
  186.             if (wantVScrollBar)
  187.             {
  188.                 itsSize.h -= kSBarSizeMinus1;
  189.                 if (!wantHScrollBar)
  190.                     sBarOffsets.bottom = -kSBarSizeMinus1;
  191.             }
  192.             aScroller = new TScroller;
  193.             aScroller->IScroller(aWindow, gZeroVPt, itsSize, sizeRelSuperView, sizeRelSuperView, gZeroVPt, wantHScrollBar, wantVScrollBar);
  194.             aScroller->fSBarOffsets = sBarOffsets;
  195.             if (itsView != NULL)
  196.                 aScroller->AddSubView(itsView);
  197.         }
  198.         else if (itsView != NULL)
  199.             aWindow->AddSubView(itsView);
  200.  
  201.         aWindow->SetTarget(itsView);
  202.  
  203.         // make sure window and subviews are the right size 
  204.         aWindow->Resize(VPoint(aWindow->fWMgrWindow->portRect[botRight] - aWindow->fWMgrWindow->portRect[topLeft]), kDontInvalidate);
  205.  
  206.         fi.Success();
  207.     }
  208.     else    // Recover
  209.     {
  210.         aWindow = (TWindow *)FreeIfObject(aWindow);
  211.         fi.ReSignal();                            // Optional. Omit to continue.
  212.     }
  213.  
  214.     return aWindow;
  215. }
  216.  
  217. //--------------------------------------------------------------------------------------------------
  218. #pragma segment MAOpen
  219.  
  220. pascal TWindow* TViewServer::NewTWindow(short itsRsrcID,
  221.                                   TDocument* itsDocument)
  222.  
  223. {
  224.     WindowPtr aWMgrWindow = NULL;
  225.     TWindow * aWindow = NULL;
  226.     Boolean canResize;
  227.     Boolean canClose;
  228.     FailInfo fi;
  229.  
  230.     VOLATILE(aWMgrWindow);
  231.  
  232.     aWMgrWindow = gApplication->GetRsrcWindow(NULL, itsRsrcID, canResize, canClose);
  233.     // GetRsrcWindow signals Failure 
  234.  
  235.     if (fi.Try())
  236.     {
  237.         aWindow = new TWindow;
  238.         fi.Success();
  239.     }
  240.     else    // Recover
  241.     {
  242.         aWMgrWindow = FreeIfWMgrWindow(aWMgrWindow, TRUE);
  243.         fi.ReSignal();                            // Optional. Omit to continue.
  244.     }
  245.  
  246.     aWindow->IWindow(itsDocument, aWMgrWindow, canResize, canClose, TRUE);/* TRUE means can dispose
  247.                                                   wmgr window */
  248.  
  249.     return aWindow;
  250. }
  251.  
  252. //--------------------------------------------------------------------------------------------------
  253. #pragma segment MAOpen
  254.  
  255. pascal TWindow* TViewServer::NewTemplateWindow(short viewRsrcID,
  256.                                          TDocument* itsDocument)
  257.  
  258. {
  259.     TWindow * theWindow = NULL;
  260.     TView * theTarget = NULL;
  261.     TView * aView = NULL;
  262.  
  263.     aView = this->DoCreateViews(itsDocument, NULL, viewRsrcID, gZeroVPt);
  264.     if (aView != NULL)
  265.     {
  266.         if (qDebug && !(aView->IsMemberClass(GetClassIDFromName("TWindow"))))
  267.             ProgramBreak("In NewTemplateWindow: Root view is not a window");
  268.  
  269.         theWindow = (TWindow *)aView;
  270.  
  271.         if (theWindow->fWMgrWindow != NULL)
  272.         {
  273.             Rect portRect(theWindow->fWMgrWindow->portRect);
  274.             theWindow->Resize(VPoint(portRect.bottom - portRect.top, portRect.right - portRect.left), kDontInvalidate);
  275.         }
  276.         if (theWindow->fTargetID != kNoIdentifier)
  277.         {
  278.             theTarget = theWindow->FindSubView(theWindow->fTargetID);
  279.             if (theTarget != NULL)
  280.                 theWindow->SetTarget(theTarget);
  281.             else if (qDebug)
  282.                 ProgramBreak("The window has no view whose id is fTargetId.");
  283.         }
  284.     }
  285.     return theWindow;
  286. }
  287.  
  288. //--------------------------------------------------------------------------------------------------
  289. #pragma segment MAOpen
  290.  
  291. pascal TView* TViewServer::DoCreateViews(TDocument* itsDocument,
  292.                                          TView* parentView,
  293.                                          short itsRsrcID,
  294.                                          const VPoint& subviewOffset)
  295. {
  296.     ViewRsrcHandle viewResource;
  297.     SignedByte savedState;
  298.     FailInfo fi;
  299.     TView * firstView = NULL;                        // Assume the worst. 
  300.  
  301.     VOLATILE(viewResource);
  302.     VOLATILE(firstView);
  303.     
  304.     viewResource = (ViewRsrcHandle)GetResource('view', itsRsrcID);
  305.     if (viewResource == NULL)
  306.     {
  307. #if qDebug
  308.         Str255 theString;
  309.         ConcatNumber("Unable to find ‘view’ resource #", itsRsrcID, theString);
  310.         ProgramBreak(theString);
  311. #endif
  312.  
  313.         FailNILResource((Handle)viewResource);
  314.     }
  315.  
  316.     savedState = LockHandleHigh((Handle)viewResource);
  317.  
  318.     if (fi.Try())
  319.     {
  320.         short numViews = (**viewResource).numViews;
  321.         IDType lastParentID = kNoIdentifier;
  322.         TView * aView = parentView;
  323.         TView * lastRoot = parentView;
  324.         TView * lastParent = NULL;
  325.  
  326.         ViewTemplatePtr theViewInfo = &((**viewResource).theViews);// Initial setting. This pointer is bumped in the for loop below
  327.         for (short i = 1; i <= numViews; ++i)
  328.         {
  329. #if qDebugMsg
  330.             if (gIntenseDebugging)
  331.                 ReportTemplate(*theViewInfo);
  332. #endif
  333.  
  334.             if (((long) theViewInfo->itsParentID) == ((long)kNoIdentifier))
  335.                 lastParent = parentView;
  336.             else if (((long)theViewInfo->itsParentID) != ((long)lastParentID))
  337.             {
  338.                 lastParent = aView;            // Begin with last view created or parentView
  339.                 while ((lastParent != NULL) && (lastParent->fIdentifier != theViewInfo->itsParentID))
  340.                     lastParent = lastParent->fSuperView;
  341.  
  342.                 if ((lastParent == NULL) && (lastRoot != NULL))
  343.                     if (aView != NULL)
  344.                         lastParent = aView->FindSubView(theViewInfo->itsParentID);
  345.                     else
  346.                         lastParent = lastRoot->FindSubView(theViewInfo->itsParentID);
  347.  
  348. #if qDebug
  349.                 if (lastParent == NULL)
  350.                     ProgramBreak("Unable to find parent view for template");
  351. #endif
  352.  
  353.             }
  354.             lastParentID = theViewInfo->itsParentID;
  355.  
  356.             if (((long)theViewInfo->itsSignature) == ((long)'incl'))
  357.             {
  358.                 aView = this->DoCreateViews(itsDocument, lastParent, theViewInfo->u1.includeRsrcID, gZeroVPt);
  359.                 OffsetPtr((Ptr&)theViewInfo, sizeof(ViewTemplate) - sizeof(Str255) + sizeof(short));
  360.             }
  361.             else if (((long)theViewInfo->itsSignature) == ((long)'inc@'))
  362.             {
  363.                 aView = this->DoCreateViews(itsDocument, lastParent, theViewInfo->u1.includeRsrcID, (const VPoint&) theViewInfo->u1.itsSubViewOffset);
  364.                 OffsetPtr((Ptr&)theViewInfo, sizeof(ViewTemplate) - sizeof(Str255) + sizeof(short) + sizeof(VPoint));
  365.             }
  366.             else
  367.                 aView = this->CreateAView(itsDocument, lastParent, (Ptr&) theViewInfo);
  368.  
  369.             if (aView == NULL)
  370.                 break;
  371.             if (((subviewOffset.h != 0) || (subviewOffset.v != 0)) && (aView->fSuperView == parentView) && (parentView != NULL))
  372.                 aView->Locate(aView->fLocation + subviewOffset, kDontInvalidate);
  373.  
  374.             if (i == 1)
  375.             {
  376.                 firstView = aView;
  377.  
  378.                 if (aView->IsMemberClass(GetClassIDFromName("TWindow")) && (parentView == NULL))
  379.                     parentView = aView;
  380.             }
  381.  
  382.             if ((lastRoot == NULL) && (aView != NULL) && (aView->fSuperView == NULL))
  383.                 lastRoot = aView;
  384.  
  385.         }                                    // for
  386.  
  387.         HSetState((Handle) viewResource, savedState);
  388.         fi.Success();
  389.     }
  390.     else                                    // Recover
  391.     {
  392.         HSetState((Handle) viewResource, savedState);
  393.  
  394.         firstView = (TView *)FreeIfObject(firstView);
  395.     }
  396.  
  397.     if (firstView)
  398.         firstView->AdjustSize();            // Make sure size gets adjusted by the size determiners 
  399.  
  400.     return firstView;
  401. }
  402.  
  403. //--------------------------------------------------------------------------------------------------
  404. #pragma segment MAOpen
  405.  
  406. pascal TView* TViewServer::CreateAView(TDocument* itsDocument,
  407.                                        TView* itsSuperView,
  408.                                        Ptr& itsParams)
  409.  
  410. {
  411.     TView * aView = NULL;
  412.  
  413.     ViewTemplatePtr params = (ViewTemplatePtr)(itsParams);
  414.     Str255 className = params->u0.itsType;    //!!! NOTE: Union
  415.  
  416.     if (className.IsEmpty())
  417.         aView = (TView *)(NewStdObject(params->itsSignature));
  418.     else
  419.     {
  420.         aView = (TView *)(NewObjectByClassName(className));
  421.         if ((aView == NULL) && (GetClassIDFromName(className) == kNilClass))
  422.         {
  423. #if qDebug
  424.             ProgramBreak("The application doesn’t contain the class ‘" + className + ".’");
  425. #endif
  426.  
  427.             gErrorParm3 = className;        // show name of class 
  428.             Failure(errMissingClass, 0);
  429.         }
  430.     }
  431.  
  432.     if (aView)
  433.         aView->IRes(itsDocument, itsSuperView, itsParams);
  434.     else
  435.         FailNIL(aView);
  436.  
  437.     return aView;
  438. }
  439.  
  440. //--------------------------------------------------------------------------------------------------
  441. #pragma segment MADebug
  442.  
  443. #if qDebugMsg
  444.  
  445. pascal void TViewServer::ReportTemplate(const ViewTemplate& theViewData)
  446. {
  447.     WrLblSig("signature", theViewData.itsSignature);
  448.     fprintf(stderr, "\n");
  449.     WrLblSig("itsParentID", theViewData.itsParentID);
  450.     WrLblSig(", thisViewID", theViewData.thisViewID);
  451.     fprintf(stderr, "\n");
  452.     WrLblVPt("itsLocation", theViewData.itsLocation);
  453.     WrLblVPt(", itsSize", theViewData.itsSize);
  454.     fprintf(stderr, "itsHSizeDet == %3ld", (long)theViewData.itsHSizeDet);
  455.     fprintf(stderr, ", itsVSizeDet == %3ld", (long)theViewData.itsVSizeDet);
  456.     WrLblBoolean(", isEnabled ", theViewData.isEnabled);
  457.     fprintf(stderr, "\n");
  458.     fprintf(stderr, "----------  end of view  ----------\n");
  459. }
  460. #endif
  461.  
  462.